-- UIScreenShining
-- Created by chengb May/14/2015
-- 全屏星光特效界面

UIScreenShining = class("UIScreenShining", function()
    return cc.Layer:create();
end);

function UIScreenShining.create()
    return UIScreenShining.new();
end

-- 界面存活时间
local DURATION = 2;

-- 构造函数
function UIScreenShining:ctor()
    -- 初始化
    self:setName("UIScreenShining");
    local node = cc.CSLoader:createNode("layout/common/ScreenShining.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER);

    -- 初始化窗口
    self:initForm();

    -- 适配
    self:resize();

    -- 定时关闭
    local function closeMe()
        UIMgr.getCurrentScene():removeFormByName("UIScreenShining");
    end
    performWithDelay(self, closeMe, DURATION);
end

-- 适配
function UIScreenShining:resize()
    local node = self.node;

    -- bg充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- panel充满整个屏幕
    AlignM.fitToScreen(node, "panel");
end

-- 初始化窗口
function UIScreenShining:initForm()
    -- 播放背景淡入淡出特效
    local bg = findChildByName(self.node, "bg");

    local initOpacity = 156;   -- 初始化透明度
    local maxOpactiy  = 255;   -- 最高透明度
    local lastOpacity = 0;     -- 最终透明度
    local fadeInTime  = 0.36;  -- 淡入时间
    local fadeOutTime = 0.64;  -- 淡出时间

    local fadeIn  = cc.FadeTo:create(fadeInTime, maxOpactiy);
    local fadeOut = cc.FadeTo:create(fadeOutTime, lastOpacity);
    bg:runAction(cc.Sequence:create(fadeIn, fadeOut));

    -- 播放星光闪烁
    local starNum = 25;   -- 星光总数
    local panel = findChildByName(self.node, "panel");
    local frameSize = cc.Director:getInstance():getOpenGLView():getFrameSize();
    local w = frameSize.width;
    local h = frameSize.height;
    local margin = 20;
    for i = 1, starNum do
        -- 随机选择坐标
        local posX = math.random(margin, w - margin);
        local posY = math.random(margin, h - margin);

        local effectNode = cc.Node:create();
        effectNode:setPosition(cc.p(posX, posY));
        panel:addChild(effectNode);

        local scale = math.random(5, 13) / 10;
        effectNode:setScale(scale);

        -- 随机延迟
        local delay = math.random(1, 11)/10;

        local function play()
            playEffect(effectNode, 2050);
        end

        performWithDelay(effectNode, play, delay);
    end
end




